package com.blue.waffle.iwc.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.blue.waffle.iwc.domains.Profile;
import com.blue.waffle.iwc.game.IWCGame;
import com.blue.waffle.iwc.screens.MenuScreen.MenuActorListener.Button;
import com.blue.waffle.iwc.ui.ButtonActorListener;
import com.blue.waffle.iwc.ui.MessageDialog;
import com.blue.waffle.iwc.ui.ParallaxBackground;
import com.blue.waffle.iwc.ui.ParallaxLayer;
import com.blue.waffle.iwc.utils.MusicManager;
import com.blue.waffle.iwc.utils.MusicManager.IWCGameMusic;


public class MenuScreen extends AbstractScreen {

	
	private static final int BUTTON_WIDTH=300;
	private static final int BUTTON_HEIGHT=50;
	
	public MenuScreen(IWCGame game) {
		super(game);
	}
	
	@Override
	public void show() {
	
		super.show();
		
		MusicManager musicManager=game.getMusicManager();
		
		musicManager.play(IWCGameMusic.MENU);
		
		Skin skin=getUiSkin();
		
		ParticleEffect bubbleEffect=game.getAssetManager().get("effects/bubble.particle",ParticleEffect.class);
		setParticleEffect(bubbleEffect);
		Table table=new Table(skin);
		table.setFillParent(true);
		gameStage.addActor(table.padTop(IWCGame.VIRTUAL_HEIGHT/2));
		TextButton startGameButton=new TextButton("Start game", skin);
		startGameButton.addListener(new MenuActorListener(this,Button.Start));
		table.defaults().right().fillX();
		table.row();
		table.add(startGameButton).size(BUTTON_WIDTH,BUTTON_HEIGHT).uniform();
		table.row();
		
		
		TextButton continueButton=new TextButton("Continue", skin);
		continueButton.addListener(new MenuActorListener(this,Button.Continue));
		table.add(continueButton).size(BUTTON_WIDTH,BUTTON_HEIGHT).uniform();
		table.row();
		
		TextButton optionsButton=new TextButton("Options", skin);
		optionsButton.addListener(new MenuActorListener(this,Button.Options));
		table.add(optionsButton).size(BUTTON_WIDTH,BUTTON_HEIGHT).uniform();
		table.row();
		
		TextButton achievementButton=new TextButton("Achievement", skin);
		achievementButton.addListener(new MenuActorListener(this,Button.Achievement));
		table.add(achievementButton).size(BUTTON_WIDTH,BUTTON_HEIGHT).uniform();
	}
	
	public static class MenuActorListener extends ButtonActorListener{
		public enum Button{Start,Continue,Options,Achievement};
		private Button type;
		
		public MenuActorListener(MenuScreen screen, Button type){
			super(screen);
			this.type=type;
		}
		
		@Override
		public void clicked(InputEvent event, float x, float y) {
			super.clicked(event, x, y);
			IWCGame game=screen.getGame();
			switch (type) {
			case Start:
				game.setScreen(new ProfileScreen(game));
				break;
			case Continue:
				Profile profile=game.getGameManager().getUserProfile();
				if (profile!=null) {
					if (profile.getCurrentDifficulty().getId()==0) {
						game.setScreen(new DifficultyScreen(game));
					}else {
						game.setScreen(new GameScreen(game, game.getGameManager().getCurrentDifficulty()));
					}
				}else {
					MessageDialog dialog=new MessageDialog("Please start a new game.","No Profile found", screen.getUiSkin());
					dialog.show(screen.getGameStage());
				}
				
				break;
			case Options:
				System.out.println("options");
				game.setScreen(new OptionsScreen(game));
				break;
			case Achievement:
				System.out.println("achievement");
				game.setScreen(new AchievementsScreen(game));
				break;
			default:
				break;
			}
		}
		
	}

	@Override
	protected void draw(float delta) {
	
		super.draw(delta);
		TextureRegion logoRegion=getTextureAtlas().findRegion("logo");
		batch.draw(logoRegion, IWCGame.VIRTUAL_WIDTH/2-logoRegion.getRegionWidth()/2, logoRegion.getRegionHeight());
	}
	
	@Override
	public boolean keyDown(int keycode) {
		if (Gdx.input.isKeyPressed(Keys.BACK)) {
			Gdx.app.exit();
			return true;
		}
		return super.keyDown(keycode);
	}
}
